<script lang="ts" setup>
/*
1：基地址使用环境变量
  vue-cli
  vite
    开发环境
       根路径创建 .env.development
       定义环境变量： 以 VITE_开头
       访问:import.meta.env.VITE_开头
    生产环境
       根路径创建 .env.production
       定义环境变量： 以 VITE_开头
       访问:import.meta.env.VITE_开头
2:返回值数据类型定义问题
3：登录失败的提示
4:登录成功存储用户信息等

*/
import { ref } from 'vue'
import type { loginPasswordReq, loginRules } from '@/types/login'
import type { FormInstance } from 'vant'
import { showFailToast } from 'vant'
import { loginPassword } from '@/api/user'
import { useUserStore } from '@/stores'
const store = useUserStore()
const show = ref(false)
const form = ref<loginPasswordReq>({
  mobile: '13211112222',
  password: 'abc12345'
})
const rules: loginRules = {
  mobile: [
    { required: true, message: '必填', trigger: 'onChange' },
    { pattern: /^1[2-9]\d{9}$/, message: '请正确输入手机号', trigger: 'onChange' }
  ],
  password: [
    { required: true, message: '必填', trigger: 'onChange' },
    { pattern: /^\w{8,18}$/, message: '请正确输入8-18位密码', trigger: 'onChange' }
  ]
}
const formCom = ref<FormInstance>()

const pass = ref(false)

const submit = async () => {
  if (!pass.value) {
    // 错误提示
    // return
    showFailToast('请先勾选协议！')
    return
  }
  await formCom.value?.validate()
  const res = await loginPassword(form.value)
  store.setUserInfo(res.data)
  console.log('登录接口返回值', res.data.token)
}
</script>

<template>
  <div class="login-page">
    <cp-nav-bar rightText="注册" @click-right="$router.push('/register')" />
    <!-- 头部 -->
    <div class="login-head">
      <h3>密码登录</h3>
      <a href="javascript:;">
        <span>短信验证码登录</span>
        <van-icon name="arrow"></van-icon>
      </a>
    </div>
    <!-- 表单 -->
    <van-form autocomplete="off" ref="formCom">
      <van-field
        :rules="rules.mobile"
        v-model="form.mobile"
        placeholder="请输入手机号"
        type="tel"
      ></van-field>
      <van-field
        :rules="rules.password"
        v-model="form.password"
        placeholder="请输入密码"
        :type="`${show ? 'text' : 'password'}`"
      >
        <template #right-icon>
          <cp-icon @click="show = !show" :name="`login-eye-${show ? 'on' : 'off'}`" />
        </template>
      </van-field>
      <div class="cp-cell">
        <van-checkbox v-model="pass">
          <span>我已同意</span>
          <a href="javascript:;">用户协议</a>
          <span>及</span>
          <a href="javascript:;">隐私条款</a>
        </van-checkbox>
      </div>
      <div class="cp-cell">
        <van-button block round type="primary" @click="submit">登 录</van-button>
      </div>
      <div class="cp-cell">
        <a href="javascript:;">忘记密码？</a>
      </div>
    </van-form>
    <!-- 底部 -->
    <div class="login-other">
      <van-divider>第三方登录</van-divider>
      <div class="icon">
        <img src="@/assets/qq.svg" alt="" />
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.login {
  &-page {
    padding-top: 46px;
  }
  &-head {
    display: flex;
    padding: 30px 30px 50px;
    justify-content: space-between;
    align-items: flex-end;
    line-height: 1;
    h3 {
      font-weight: normal;
      font-size: 24px;
    }
    a {
      font-size: 15px;
    }
  }
  &-other {
    margin-top: 60px;
    padding: 0 30px;
    .icon {
      display: flex;
      justify-content: center;
      img {
        width: 36px;
        height: 36px;
        padding: 4px;
      }
    }
  }
}
.van-form {
  padding: 0 14px;
  .cp-cell {
    height: 52px;
    line-height: 24px;
    padding: 14px 16px;
    box-sizing: border-box;
    display: flex;
    align-items: center;
    .van-checkbox {
      a {
        color: var(--cp-primary);
        padding: 0 5px;
      }
    }
  }
  .btn-send {
    color: var(--cp-primary);
    &.active {
      color: rgba(22, 194, 163, 0.5);
    }
  }
}
</style>
